package com.itheima.demo2;

import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;

public class StreamDemo6 {
    public static void main(String[] args) {

        /**
         * collect(Collector collector)   收集流中的数据，放到集合中（list set Map）
         *
         */

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"张无忌-男-15","周芷若-女-18","赵敏-女-16","张强-男-20","高启强-男-35","张三丰-男-100","老莫-男-38","谢逊-男-38");

        //收集到list集合中
        List<String> collect = list.stream().map(s -> s.split("-")[1]).collect(Collectors.toList());
        System.out.println(collect);



        //收集到set集合中
        Set<String> collect1 = list.stream().map(s -> s.split("-")[1]).collect(Collectors.toSet());
        System.out.println(collect1);

        //收集男性到map集合中
        Map<String, Integer> map = list.stream().filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toMap(new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        //返回键
                        return s.split("-")[0];
                    }
                }, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        //返回年龄
                        return Integer.parseInt(s.split("-")[2]);
                    }
                }));

        System.out.println(map);

        //lambda表达式简化map集合
        Map<String, Integer> map1 = list.stream().filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toMap(
                        s -> s.split("-")[0],
                        s -> Integer.parseInt(s.split("-")[2])
                ));

        System.out.println(map1);
    }
}
